<template>
  <el-dialog
    :title="isEditing ? '修改' : '新增'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="物料编号" prop="length">
        <el-input v-model="dataForm.productno" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input v-model="dataForm.length" placeholder=""></el-input>
      </el-form-item>
    <el-form-item label="宽度" prop="width">
      <el-input v-model="dataForm.width" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="厚度" prop="thickness">
      <el-input v-model="dataForm.thickness" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="重量" prop="weight">
      <el-input v-model="dataForm.weight" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="materialtypegroup">
      <el-input v-model="dataForm.materialtypegroup" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          productno: '',
          length: '',
          width: '',
          thickness: '',
          weight: '',
          materialtypegroup: '',
          isEditing: false, // 新增的变量
        },
        dataRule: {
          length: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          width: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          thickness: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          materialtypegroup: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.isEditing = !!id; // 如果id存在，则设置为true, 否则为false
        this.dataForm.productno = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.productno) {
            this.$http({
              url: this.$http.adornUrl(`/materialcopy/materialtestcopy1/info/${this.dataForm.productno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.length = data.materialTestCopy1.length
                this.dataForm.width = data.materialTestCopy1.width
                this.dataForm.thickness = data.materialTestCopy1.thickness
                this.dataForm.weight = data.materialTestCopy1.weight
                this.dataForm.materialtypegroup = data.materialTestCopy1.materialtypegroup
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/materialcopy/materialtestcopy1/${this.isEditing?'update':'save'}`),
              method: 'post',
              data: this.$http.adornData({
                'productno': this.dataForm.productno,
                'length': this.dataForm.length,
                'width': this.dataForm.width,
                'thickness': this.dataForm.thickness,
                'weight': this.dataForm.weight,
                'materialtypegroup': this.dataForm.materialtypegroup
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
